package k8s

import (
	"context"

	extensions "k8s.io/api/extensions/v1beta1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/labels"
)

func (k *K8sClient) ListIngressByLabels(namespace string, labelsMap map[string]string) ([]extensions.Ingress, error) {
	options := metav1.ListOptions{
		LabelSelector: labels.SelectorFromSet(labels.Set(labelsMap)).String(),
	}

	ingressList, err := k.Clientset.ExtensionsV1beta1().Ingresses(namespace).List(context.TODO(), options)
	if err != nil {
		return nil, err
	}

	return ingressList.Items, nil
}

func (k *K8sClient) DeleteIngressByLabels(namespace string, labelsMap map[string]string, gracePeriod int) error {
	gracePeriodSeconds := int64(gracePeriod)
	deleteOpts := metav1.DeleteOptions{
		GracePeriodSeconds: &gracePeriodSeconds,
	}

	listOpts := metav1.ListOptions{
		LabelSelector: labels.SelectorFromSet(labelsMap).String(),
	}

	return k.Clientset.ExtensionsV1beta1().Ingresses(namespace).DeleteCollection(context.TODO(), deleteOpts, listOpts)
}
